package com.zhangwei.expands.recycler;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.base.lib.utils.SuperToastUtils;
import com.zhangwei.expands.R;
import com.zhangwei.expands.adapter.MyRecyclerAdapter;
import com.zhangwei.expands.recycler.decoration.DividerItemDecoration;

import java.util.LinkedList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * RecyclerView  5.0新控件     兼容到1.6的话使用v7
 * 相当于   5.0以下
 * 版本的ListView   GridView   staggerGridView(瀑布流)
 *
 * 使用条件
 * 1. v7 必须是版本21以上
 *
 *
 * 确定
 *
 * item的点击事件 默认没有实现
 * 没有分隔条   需要自己去实现
 */
public class RecyclerViewFragment extends Fragment implements MyRecyclerAdapter.OnRecyclerItemClick {
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.add)
    Button add;
    @Bind(R.id.remove)
    Button remove;
    private MyRecyclerAdapter rcAdapter;
    private LinkedList<String> list;

    public static RecyclerViewFragment newInstance() {
        Bundle args = new Bundle();
        RecyclerViewFragment fragment = new RecyclerViewFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_recycler_view, container, false);
        ButterKnife.bind(this, view);
        initView();
        initData();
        return view;
    }

    /**
     * 第一步 导入recyclerView  library
     * 第二步 声明控件,初始化控件
     * 第三步
     */
    private void initView() {
        /**
         *
         * 参数一 上下文
         * 参数二  方向  LinearLayoutManager.VERTICAL 垂直
         *             LinearLayoutManager.HORIZONTAL 水平
         *参数三
         *
         */

        RecyclerView.LayoutManager layoutmanager = getLinearLayoutManager();
//        RecyclerView.LayoutManager layoutmanager = getGridLayoutManager();
//        RecyclerView.LayoutManager layoutmanager = getStaggeredGridLayoutManager();
        recyclerView.setLayoutManager(layoutmanager);
        list = new LinkedList<>();
        for (int i = 0; i < 50; i++) {
            list.add("Recycler" + i);
        }
        rcAdapter = new MyRecyclerAdapter(list);
        rcAdapter.setItemClick(this);
        //添加第三方的实现的分隔条
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(rcAdapter);
    }


    private void addItem() {
        list.addFirst("Recycler  + add");
        rcAdapter.notifyItemInserted(0);
    }


    private void removeItem() {
        list.remove(0);
        rcAdapter.notifyItemRemoved(0);
    }


    private void initData() {
            
    }

    /**
     * 实现类似于listView的效果
     *
     * @return
     */
    private LinearLayoutManager getLinearLayoutManager() {
        LinearLayoutManager llm = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, true);
        return llm;

    }

    /**
     * 实现类似于GridView的效果
     *
     * @return
     */
    private GridLayoutManager getGridLayoutManager() {
        GridLayoutManager glm = new GridLayoutManager(getActivity(), 1);
        glm.setOrientation(LinearLayoutManager.HORIZONTAL);
        return glm;
    }

    private StaggeredGridLayoutManager getStaggeredGridLayoutManager() {
        StaggeredGridLayoutManager sgm = new StaggeredGridLayoutManager(2, LinearLayoutManager.VERTICAL);
        return sgm;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.add, R.id.remove})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add:
                addItem();

                break;
            case R.id.remove:
                removeItem();
                break;
        }
    }

    @Override
    public void onRecyclerItemClick(MyRecyclerAdapter.MyViewHolder holder, String str, int position) {
        SuperToastUtils.showShort(str);
    }
}
